package cn.shiwei.org.controller;

import cn.shiwei.basic.util.PageList;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.org.domain.Department;
import cn.shiwei.org.query.DepartmentQuery;
import cn.shiwei.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @RestController = @Controller + @ResponseBody
 * @author
 */
@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    @ApiModelProperty(value = "一个变量", dataType = "服务层接口成员变量")
    private IDepartmentService departmentService;

    /**
     * @GetMapping // get请求 {id}表示一个名为id的占位符,
     * @PathVariable // 表示对应占位符, 将参数赋值给参数
     * @param id
     * @return 返回查询结果
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门",notes = "需要参数id")
    private Department queryById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询全部",notes = "无参数")
    private List<Department> queryAll(){
        return departmentService.loadAll();
    }

    /**
     * 分页查询
     * @RequestBody : 前端的对象数据是封装到了请求体中, 从请求体中取数据用 @RequestBody注解
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "部门列表分页查询",notes = "需要传参 为高级查询的条件")
    private PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        System.out.println(query);
        return departmentService.queryDate(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除删除",notes = "需要参数id")
    private RequestStatus delete(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }

    @PutMapping()
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    private  RequestStatus addAndUpdate(@RequestBody Department department){
        System.out.println(department);
        try {
            if(department.getId() == null){
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("操作失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "根据id批量删除",notes = "需要参数id")
    private RequestStatus patchDelete(@RequestBody List<Long> ids){
        try {
            System.out.println(ids);
            departmentService.patchRemove(ids);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "查询部门结构", notes = "不需要参数")
    private List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }
}
